package com.nut.gen;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.template.*;
import com.nut.entity.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

public class Generator {

    public static final String BASE_PACKAGE = "com.nut";
    public static final String ROOT_PACKAGE = "src.main.java.com.nut";
    public static final String ROOT_RESOURCE = "src.main.resources";
    public static final String mapper = "mapper";
    public static final String mapperXml = "mapper";
    public static final String service = "service";
    public static final String entity = "entity";
    public static final String serviceImpl = "service.impl";
    public static final String controller = "api.biz";

    public static final List<TemplateFileConfig> templateFileConfigs = new ArrayList<>();

    public void init() {
        templateFileConfigs.add(new TemplateFileConfig(mapperXml, "MapperXml", "Mapper.xml", false));
        templateFileConfigs.add(new TemplateFileConfig(mapper, "Mapper", "Mapper.java", true));
        templateFileConfigs.add(new TemplateFileConfig(service, "Service", "Service.java", true));
        templateFileConfigs.add(new TemplateFileConfig(serviceImpl, "ServiceImpl", "ServiceImpl.java", true));
        templateFileConfigs.add(new TemplateFileConfig(controller, "Controller", "Controller.java", true));
    }

    private void genFile(File file, Template template, Map<String, Object> params) {
        // 生成目标文件
        Writer writer = null;
        try {
            FileUtil.touch(file);
            writer = new FileWriter(file);
            template.render(params, writer);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            assert writer != null;
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public StringBuilder getBaseStringBuilder(boolean javaPath) {
        String rootPath = System.getProperty("user.dir");

        StringBuilder sb = new StringBuilder();
        sb.append(rootPath).append(File.separator);

        if (javaPath) {
            String basePackage = ROOT_PACKAGE.replace(".", File.separator);
            sb.append(basePackage);
        } else {
            String basePackage = ROOT_RESOURCE.replace(".", File.separator);
            sb.append(basePackage);
        }
        sb.append(File.separator);
        String ss = sb.toString();
        System.out.println(ss);
        return sb;
    }

    public void gen(Class clazz) {

        String simpleName = clazz.getSimpleName();
        String paramName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);

        HashMap<String, Object> params = new HashMap<>();
        params.put("simpleName", simpleName);
        params.put("paramName", paramName);
        params.put("package", BASE_PACKAGE);
        params.put("mapper", mapper);
        params.put("service", service);
        params.put("serviceImpl", serviceImpl);
        params.put("controller", controller);
        params.put("entity", entity);

        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));

        for (TemplateFileConfig config : templateFileConfigs) {
            StringBuilder sb = getBaseStringBuilder(config.isJavaPath());

            sb.append(config.getSubPackage().replace(".", File.separator));
            sb.append(File.separator);
            sb.append(simpleName);
            sb.append(config.getTargetNameSuffix());
            String filePath = sb.toString();
            System.out.println(filePath);

            File file = new File(filePath);

            // 如果非覆盖生成
//            if (file.exists()) {
//                System.out.println("file_exist=" + filePath);
//                continue;
//            }

            Template template = engine.getTemplate(config.getTemplateName() + ".ftl");

            // 生成代码
            genFile(file, template, params);
        }

    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Generator generator = new Generator();
        generator.init();

        generator.gen(MemberGoodsCollect.class);
        System.out.println("dd");


//        Class<Activity> clazz = Activity.class;
//        String simpleName = clazz.getSimpleName();
//        String name = clazz.getName();
//        System.out.println(simpleName);
//        System.out.println(name);
//        String dd = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
//        System.out.println(dd);
    }

}
